<?php

/**
 * Description of respond
 *
 * @author asus
 */
class Respondmodel extends CI_Model {

    //CUSTOMER
    //add a customer
    public function add_customer($csr_id = null, $customer_notes = '-') {
        if ($csr_id !== null) {
            $save_customer = array(
                'csr_id' => $csr_id,
                'notes' => $customer_notes
            );

            $save = $this->db->insert('customer', $save_customer);
            return $save;
        }
    }

    //update a customer
    public function update_customer($customer_id = null, $customer_notes = null) {
        if ($customer_id !== null) {

            $stack = array();

            if ($customer_notes !== null) {
                $stack['notes'] = $customer_notes;
            }

            if ($stack !== null) {
                $this->db->where('customer_id', $customer_id);
                return $this->db->update('customer', $stack);
            }
        }
    }

    //get all data customer
    public function get_all_customer($where = null, $order_by = null, $limit = 10000, $offset = 0) {
        $this->db->from('customer');

        if ($where !== null)
            $this->db->where($where);
        if ($order_by !== null)
            $this->db->order_by($order_by);

        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get();

        return $query;
    }

    //get info from a customer id
    public function get_info_customer($customer_id = null) {
        if ($customer_id !== null) {
            return $this->get_all_customer(array('customer_id' => $customer_id), null, 1);
        }
    }

    //delete a customer
    public function delete_customer($customer_id = null) {
        if ($customer_id !== null)
            $this->db->delete('customer', array('customer_id' => $customer_id));
        return $this->db->affected_rows();
    }

    //delete table customer
    public function delete_table_customer() {
        return $this->db->delete('customer');
    }

    //empty table customer
    public function empty_table_customer() {
        $this->db->empty_table('customer');
    }

    public function get_last_id_customer() {
        $this->db->select('customer_id');
        $this->db->from('customer');
        $this->db->order_by('customer_id', 'desc');
        $query = $this->db->get()->row();

        return $query->customer_id;
    }

    //RESPOND
    //add a respond
    public function add_respond($customer_id = null, $question_id = null, $satisfy_level = null) {
        if (($customer_id !== null) && ($question_id !== null) && ($satisfy_level !== null)) {
            $add_respond = array(
                'customer_id' => $customer_id,
                'question_id' => $question_id,
                'satisfy_level' => $satisfy_level,
            );

            $save = $this->db->insert('respond', $add_respond);

            $this->load->model('questionmodel');
            $this->questionmodel->set_lock($question_id);

            return $save;
        }
    }

    //add an array of question from a customer
    public function add_all_respond($csr_id = null, $arrOfarrRespon = null) {
        if ($csr_id != null && $arrOfarrRespon !== null) {

            $this->add_customer($csr_id);
            $cust_id = $this->get_last_id_customer();

            foreach ($arrOfarrRespon as $value) {
                $this->add_respond($cust_id, $value[0], $value[1]);
            }
        }
    }

    //delete a respond
    public function delete_respond($respond_id = null) {
        if ($respond_id !== null)
            $this->db->delete('respond', array('respond_id' => $respond_id));
        return $this->db->affected_rows();
    }

    //update a respond
    public function update_respond($respond_id = null, $customer_id = null, $question_id = null, $satisfy_level = null) {
        if ($respond_id !== null) {

            $stack = array();

            if ($customer_id !== null) {
                $stack['customer_id'] = $customer_id;
            }

            if ($question_id !== null) {
                $stack['question_id'] = $question_id;
            }

            if ($satisfy_level !== null) {
                $stack['satisfy_level'] = $satisfy_level;
            }

            if ($stack !== null) {
                $this->db->where('respond_id', $respond_id);
                return $this->db->update('respond', $stack);
            }
        }
    }

    //get all respond
    public function get_all_respond($where = null, $order_by = null, $limit = 10000, $offset = 0) {
        $this->db->from('respond');

        if ($where !== null)
            $this->db->where($where);
        if ($order_by !== null)
            $this->db->order_by($order_by);

        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get();

        return $query;
    }

    public function get_respond($respond_id = null) {
        if ($respond_id !== null) {
            return $this->get_all_respond(array('respond_id' => $respond_id), null, 1);
        }
    }

    public function get_all_respond_by_questionId($question_id = null) {
        if ($question_id !== null) {
            return $this->get_all_respond(array('question_id' => $question_id));
        }
    }

    //get all respond by a customer id
    public function get_all_respond_by_customerId($customer_id = null) {
        if ($customer_id !== null) {
            return $this->get_all_respond(array('customer_id' => $customer_id));
        }
    }
    
    public function get_year_available($question_id=null, $plasa_id = null, $csr_id = null){
        if($question_id!==null){
            
            $this->db->select("distinct(DATE_FORMAT(customer.datetime,'%Y-%m')) AS result", FALSE);
            $this->db->from('respond');

            $conditional = '';
            if(is_array($question_id)){
                $conditional .= 'respond`.`question_id` = '.implode(' OR `respond`.`question_id` = ', $question_id).' ';
            }else{
                $conditional .= 'respond`.`question_id` ='.$question_id.' ';

            }
			
			if(!is_null($csr_id))  $conditional .= 'AND `customer`.`csr_id` ='.$csr_id.' ';
			else {
				if(!is_null($plasa_id))  $conditional .= 'AND `csr`.`plasa_id` ='.$plasa_id.' ';
			}

            $this->db->where($conditional);
            $this->db->join('customer', 'respond.customer_id = customer.customer_id', 'INNER JOIN');
			
            if(!is_null($plasa_id) && is_null($csr_id)) $this->db->join('csr', 'csr.csr_id = customer.csr_id', 'INNER JOIN');

            $all_respond = $this->db->get();
            //echo $this->db->last_query();


            $array = array();
            foreach ($all_respond->result() as $respond) {
                $var = $respond->result;
                $year = substr($var, 0, 4);
                $month = substr($var, -2);
                if (key_exists($year, $array)) {
                    array_push($array[$year], $month);
                } else {
                    $array[$year] = array();
                    array_push($array[$year], $month);
                }
            }
            return $array;
        }
    }

}

?>
